﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SimpleTCP;
using SimpleTCP.Server;
using HalconDotNet;
using System.IO;
using System.Threading;

namespace HalconAssit
{
    public partial class MainForm : Form
    {
        SimpleTcpServer server;
        HObject backImage;
        Dictionary<string, ClsVar> clsVarsDict = new Dictionary<string, ClsVar>();
        public MainForm()
        {
            InitializeComponent();

            //dev_set_draw('margin')

            IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
            int port = 57572;
            server = new SimpleTcpServer();
            server.StringEncoder = System.Text.ASCIIEncoding.UTF8;
            server.Delimiter = Encoding.ASCII.GetBytes(";")[0];
            server.DelimiterDataReceived += Server_DelimiterDataReceived;
            server.DataReceived += Server_DataReceived;
            server.Start(ipAddress, port);
            hSmartWindowControl1.MouseWheel += HSmartWindowControl1_MouseWheel;
            this.dgVar.Rows.Clear();

        }

        private void HSmartWindowControl1_MouseWheel(object sender, MouseEventArgs e)
        {
            HSmartWindowControl hSmartWindowControl1 = (HSmartWindowControl)sender;
            System.Drawing.Point pt = this.Location;
            int leftBorder = hSmartWindowControl1.Location.X;
            int rightBorder = hSmartWindowControl1.Location.X + hSmartWindowControl1.Size.Width;
            int topBorder = hSmartWindowControl1.Location.Y;
            int bottomBorder = hSmartWindowControl1.Location.Y + hSmartWindowControl1.Size.Height;
            // if (e.X > leftBorder && e.X < rightBorder && e.Y > topBorder && e.Y < bottomBorder)
            {
                MouseEventArgs newe = new MouseEventArgs(e.Button, e.Clicks,
                                                     (e.X - pt.X), (e.Y - pt.Y), e.Delta);
                hSmartWindowControl1.HSmartWindowControl_MouseWheel(sender, newe);
            }
        }

        private void Server_DataReceived(object sender, SimpleTCP.Message e)
        {

        }
        string ConvertDispName(string name)
        {
            if (!cbkAutoRename.Checked == true)
            {
                return name;
            }
            if (!clsVarsDict.ContainsKey(name))
            {
                return name;
            }
            else
            {
                for (int i = 1; i < 64; i++)
                {
                    string tmp = name + i.ToString();
                    if (!clsVarsDict.ContainsKey(tmp))
                    {
                        return tmp
                            ;
                    }
                }
                return name + DateTime.Now.ToString();
            }

        }
        private void Server_DelimiterDataReceived(object sender, SimpleTCP.Message e)
        {
            if (!string.IsNullOrEmpty(e.MessageString))
            {
                string[] arr = e.MessageString.Split(',');
                if (arr != null && arr.Length >= 2)
                {
                    string fname = arr[0];
                    string vartype = arr[1];
                    if (vartype == "0")
                    {

                        HObject tmp;
                        HTuple ImageWidth, ImageHeight;
                        HOperatorSet.SetSystem("clip_region", "false");
                        HOperatorSet.ReadObject(out tmp, fname);
                        string varName = Path.GetFileNameWithoutExtension(fname);
                        varName = ConvertDispName(varName);
                        //HOperatorSet.ClearWindow(hSmartWindowControl1.HalconWindow);
                        ClsVar tmpvar = null;
                        if (IsEmptyObject(tmp))
                        {
                            tmpvar = new ClsVar();
                        }
                        else if (HobjIsImage(tmp))
                        {
                            HOperatorSet.GetImageSize(tmp, out ImageWidth, out ImageHeight);
                            HOperatorSet.SetPart(hSmartWindowControl1.HalconWindow, 0, 0, ImageHeight - 1, ImageWidth - 1);
                            tmpvar = new ClsVar(new HImage(tmp));

                        }
                        else if (HobjIsRegion(tmp))
                        {
                            tmpvar = new ClsVar(new HRegion(tmp));
                        }
                        else if (HobjIsXLD(tmp))
                        {

                            tmpvar = new ClsVar(new HXLD(tmp));
                        }
                        if (tmpvar != null)
                        {
                            tmpvar.Name = varName;
                            if (!clsVarsDict.ContainsKey(tmpvar.Name))
                            {
                                clsVarsDict.Add(varName, tmpvar);
                                this.Invoke(new Action(() =>
                                {
                                    int index = this.dgVar.Rows.Add();
                                    this.dgVar.Rows[index].Cells[0].Value = varName;
                                    this.dgVar.Rows[index].Cells[1].Value = VarImg(tmpvar);
                                    this.dgVar.Rows[index].Cells[2].Value = Color2Str(Color.White);
                                    this.dgVar.Rows[index].Cells[2].Style.BackColor = Color.White;
                                }));
                            }
                            else
                            {
                                clsVarsDict[varName].Data?.Dispose();
                                clsVarsDict[varName] = tmpvar;


                                this.Invoke(new Action(() =>
                                {
                                    int i = 0;
                                    for (i = 0; i < this.dgVar.Rows.Count; i++)
                                    {
                                        if (this.dgVar.Rows[i].Cells[0].Value.ToString() == varName)
                                        {
                                            this.dgVar.Rows.RemoveAt(i);
                                            break;
                                        }
                                    }
                                    int index = this.dgVar.Rows.Add();
                                    this.dgVar.Rows[index].Cells[0].Value = varName;
                                    this.dgVar.Rows[index].Cells[1].Value = VarImg(tmpvar);
                                    this.dgVar.Rows[index].Cells[2].Value = Color2Str(Color.White);
                                    this.dgVar.Rows[index].Cells[2].Style.BackColor = Color.White;
                                }));

                            }
                            if (tmpvar.VarType != enumVarType.EMPTY)
                            {
                                DisPlayObj(tmpvar);
                            }



                        }


                    }
                    else if (vartype == "1")
                    {
                        HTuple hTuple;
                        HOperatorSet.ReadTuple(fname, out hTuple);
                        ClsVar tmpvar = null;
                        tmpvar = new ClsVar(hTuple);
                        string varName = Path.GetFileNameWithoutExtension(fname);
                        varName = ConvertDispName(varName);
                        if (tmpvar != null)
                        {
                            tmpvar.Name = varName;
                            if (!clsVarsDict.ContainsKey(tmpvar.Name))
                            {
                                clsVarsDict.Add(varName, tmpvar);
                                this.Invoke(new Action(() =>
                                {
                                    int index = this.dgVar.Rows.Add();
                                    this.dgVar.Rows[index].Cells[0].Value = varName;
                                    this.dgVar.Rows[index].Cells[1].Value = VarImg(tmpvar);
                                    this.dgVar.Rows[index].Cells[2].Value = Color2Str(Color.White);
                                    this.dgVar.Rows[index].Cells[2].Style.BackColor = Color.White;
                                }));
                            }
                            else
                            {
                                clsVarsDict[varName].Data?.Dispose();
                                clsVarsDict[varName] = tmpvar;


                                this.Invoke(new Action(() =>
                                {
                                    int i = 0;
                                    for (i = 0; i < this.dgVar.Rows.Count; i++)
                                    {
                                        if (this.dgVar.Rows[i].Cells[0].Value.ToString() == varName)
                                        {
                                            this.dgVar.Rows.RemoveAt(i);
                                            break;
                                        }
                                    }
                                    int index = this.dgVar.Rows.Add();
                                    this.dgVar.Rows[index].Cells[0].Value = varName;
                                    this.dgVar.Rows[index].Cells[1].Value = VarImg(tmpvar);
                                    this.dgVar.Rows[index].Cells[2].Value = Color2Str(Color.White);
                                    this.dgVar.Rows[index].Cells[2].Style.BackColor = Color.White;
                                }));

                            }




                        }

                    }
                    else if (vartype == "2")
                    {
                        HTuple hTuple, hv_Status;
                        HOperatorSet.ReadObjectModel3d(fname, "m", new HTuple(), new HTuple(),
                                                out hTuple, out hv_Status);

                        ClsVar tmpvar = null;
                        tmpvar = new ClsVar(hTuple);
                        string varName = Path.GetFileNameWithoutExtension(fname);
                        varName = ConvertDispName(varName);
                        if (tmpvar != null)
                        {
                            tmpvar.Name = varName;
                            if (!clsVarsDict.ContainsKey(tmpvar.Name))
                            {
                                clsVarsDict.Add(varName, tmpvar);
                                this.Invoke(new Action(() =>
                                {
                                    int index = this.dgVar.Rows.Add();
                                    this.dgVar.Rows[index].Cells[0].Value = varName;
                                    this.dgVar.Rows[index].Cells[1].Value = VarImg(tmpvar);
                                    this.dgVar.Rows[index].Cells[2].Value = Color2Str(Color.White);
                                    this.dgVar.Rows[index].Cells[2].Style.BackColor = Color.White;
                                }));
                            }
                            else
                            {
                                clsVarsDict[varName].Data?.Dispose();
                                clsVarsDict[varName] = tmpvar;


                                this.Invoke(new Action(() =>
                                {
                                    int i = 0;
                                    for (i = 0; i < this.dgVar.Rows.Count; i++)
                                    {
                                        if (this.dgVar.Rows[i].Cells[0].Value.ToString() == varName)
                                        {
                                            this.dgVar.Rows.RemoveAt(i);
                                            break;
                                        }
                                    }
                                    int index = this.dgVar.Rows.Add();
                                    this.dgVar.Rows[index].Cells[0].Value = varName;
                                    this.dgVar.Rows[index].Cells[1].Value = VarImg(tmpvar);
                                    this.dgVar.Rows[index].Cells[2].Value = Color2Str(Color.White);
                                    this.dgVar.Rows[index].Cells[2].Style.BackColor = Color.White;
                                }));

                            }




                        }

                    }
                    File.Delete(fname);
                }
            }
        }
        Bitmap VarImg(ClsVar clsVar)
        {
            if (clsVar != null)
            {
                switch (clsVar.VarType)
                {
                    case enumVarType.IMG:
                        return ImgResource.IMG;
                    case enumVarType.REGION:
                        return ImgResource.REGION;
                    case enumVarType.XLD:
                        return ImgResource.XLD;
                    case enumVarType.TUPLE:
                        return ImgResource.TUPLE;
                    case enumVarType.UNDEF:
                        return ImgResource.UNDEF;
                    case enumVarType.EMPTY:
                        return ImgResource.EMPTY;
                    case enumVarType.TUPLE3D:
                        return ImgResource._3D;
                    default:
                        return ImgResource.UNDEF;

                }
            }
            return ImgResource.UNDEF;

        }
        bool HobjIsImage(HObject obj)
        {
            bool res = false;
            try
            {
                HImage tmp = new HImage(obj);
                return true;
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        bool HobjIsRegion(HObject obj)
        {
            bool res = false;
            try
            {
                HRegion tmp = new HRegion(obj);
                return true;
            }
            catch (Exception ex)
            {
            }
            return res;

        }

        bool HobjIsXLD(HObject obj)
        {

            bool res = false;
            try
            {
                HXLD tmp = new HXLD(obj);
                return true;
            }
            catch (Exception ex)
            {
            }
            return res;

        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            HOperatorSet.SetSystem("clip_region", "false");
            this.Text = "HalconAssit.GUI Version:" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
            toolStripComboBox1.SelectedIndexChanged += ToolStripComboBox1_SelectedIndexChanged;
        }

        private void ToolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.dgVar.SelectedCells.Count > 0)
            {

                foreach (DataGridViewCell item in this.dgVar.SelectedCells)
                {
                    string vname = this.dgVar.Rows[item.RowIndex].Cells[0].Value.ToString();
                    if (this.clsVarsDict.ContainsKey(vname))
                    {
                        var tmpvar = this.clsVarsDict[vname];
                        if (tmpvar.VarType == enumVarType.REGION || tmpvar.VarType == enumVarType.XLD)
                        {
                            if (toolStripComboBox1.SelectedIndex >= 0)
                            {
                                if (toolStripComboBox1.SelectedIndex == 0)
                                {
                                    tmpvar.RegionDrawMode = "fill";
                                }
                                else
                                {
                                    tmpvar.RegionDrawMode = "margin";
                                }
                            }

                        }

                    }
                }


            }
        }

        private void SaveVarData(ClsVar clsVar, string path)
        {
            if (clsVar.VarType == enumVarType.IMG || clsVar.VarType == enumVarType.REGION || clsVar.VarType == enumVarType.XLD)
            {
                HOperatorSet.WriteObject(clsVar.Data, Path.Combine(path, clsVar.Name + ".hobj"));
            }

        }

        HTuple ImageWidth, ImageHeight;
        private void button1_Click(object sender, EventArgs e)
        {

            OpenFileDialog openFile = new OpenFileDialog();
            openFile.InitialDirectory = System.Environment.CurrentDirectory + "\\Image";
            openFile.Title = "选择模板图片";
            openFile.ShowDialog();
            if (!(openFile.FileName == ""))
            {
                try
                {
                    backImage = null;
                    HOperatorSet.ReadImage(out backImage, openFile.FileName);
                    HOperatorSet.ClearWindow(hSmartWindowControl1.HalconWindow);
                    HOperatorSet.GetImageSize(backImage, out ImageWidth, out ImageHeight);
                    HOperatorSet.SetPart(hSmartWindowControl1.HalconWindow, 0, 0, ImageHeight - 1, ImageWidth - 1);
                    HOperatorSet.DispObj(backImage, hSmartWindowControl1.HalconWindow);
                }
                catch (Exception e1)
                {

                }
            }
        }

        private void hSmartWindowControl1_HMouseDown(object sender, HMouseEventArgs e)
        {
            HTuple Row, Column, Button, pointGray, Wx, Wy;
            try
            {
                HOperatorSet.GetMposition(hSmartWindowControl1.HalconWindow, out Row, out Column, out Button);              //获取当前鼠标的坐标值

            }
            catch (Exception)
            {
                Row = 0; Column = 0; Button = 0; pointGray = "_";
                Wx = 0; Wy = 0;
            }

            if (backImage != null)//&& (Row > 0 && Row < ImageHeight) && (Column > 0 && Column < ImageWidth))//设置3个条件项，防止程序崩溃。
            {
                try
                {
                    HOperatorSet.GetImageSize(backImage, out ImageWidth, out ImageHeight);
                    HOperatorSet.GetGrayval(backImage, Row, Column, out pointGray);                 //获取当前点的灰度值
                }
                catch (Exception)
                {
                    pointGray = "_";
                }
            }

            else
            {
                pointGray = "_";
            }



            String str = String.Format("Row:{0}  Column:{1}  Gray:{2} ", Row, Column, pointGray); //格式化字符串

            labStatusXYGray.Text = str;
            Row.Dispose(); Column.Dispose(); Button.Dispose(); pointGray.Dispose();
        }

        private void hSmartWindowControl1_HMouseWheel(object sender, HMouseEventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            HOperatorSet.ClearWindow(hSmartWindowControl1.HalconWindow);
        }
        /// <summary>
        /// 废弃不调用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void hSmartWindowControl1_MouseDown(object sender, MouseEventArgs e)
        {
            HTuple Row, Column, Button, pointGray, Wx, Wy;
            try
            {
                HOperatorSet.GetMposition(hSmartWindowControl1.HalconWindow, out Row, out Column, out Button);              //获取当前鼠标的坐标值

            }
            catch (Exception)
            {
                Row = 0; Column = 0; Button = 0; pointGray = "_";
                Wx = 0; Wy = 0;
            }

            if (backImage != null && (Row > 0 && Row < ImageHeight) && (Column > 0 && Column < ImageWidth))//设置3个条件项，防止程序崩溃。
            {
                try
                {
                    HOperatorSet.GetImageSize(backImage, out ImageWidth, out ImageHeight);
                    HOperatorSet.GetGrayval(backImage, Row, Column, out pointGray);                 //获取当前点的灰度值
                }
                catch (Exception)
                {
                    pointGray = "_";
                }
            }

            else
            {
                pointGray = "_";
            }



            String str = String.Format("Row:{0}  Column:{1}  Gray:{2} ", Row, Column, pointGray); //格式化字符串

            labStatusXYGray.Text = str;
            Row.Dispose(); Column.Dispose(); Button.Dispose(); pointGray.Dispose();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            ClearAllVar(this.clsVarsDict);
            HOperatorSet.ClearWindow(hSmartWindowControl1.HalconWindow);
        }
        string Color2Str(Color color)
        {
            Color colorChoosed = color;
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("#");
            stringBuilder.Append(colorChoosed.R.ToString("X").PadLeft(2, '0'));
            stringBuilder.Append(colorChoosed.G.ToString("X").PadLeft(2, '0'));
            stringBuilder.Append(colorChoosed.B.ToString("X").PadLeft(2, '0'));
            stringBuilder.Append(colorChoosed.A.ToString("X").PadLeft(2, '0'));
            return stringBuilder.ToString();
        }
        HTuple OlddrawMode = new HTuple("fill");
        void DrawMode_Save()
        {
            HOperatorSet.GetDraw(hSmartWindowControl1.HalconWindow, out OlddrawMode);

        }
        void DrawMode_ReStore()
        {
            HOperatorSet.SetDraw(hSmartWindowControl1.HalconWindow, OlddrawMode);
        }

        private void dgVar_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dgVar.Columns[e.ColumnIndex].Name == "ColorDef" && e.RowIndex >= 0)
            {

                string vname = this.dgVar.Rows[e.RowIndex].Cells[0].Value.ToString();
                if (this.clsVarsDict.ContainsKey(vname))
                {
                    var tmpvar = this.clsVarsDict[vname];
                    if (tmpvar.VarType == enumVarType.REGION || tmpvar.VarType == enumVarType.XLD || tmpvar.VarType == enumVarType.TUPLE3D)
                    {
                        ColorDialog colorDia = new ColorDialog();
                        if (colorDia.ShowDialog() == DialogResult.OK)
                        {
                            //获取所选择的颜色
                            Color colorChoosed = colorDia.Color;
                            tmpvar.Color = Color2Str(colorChoosed);
                            this.dgVar.Rows[e.RowIndex].Cells[2].Value = tmpvar.Color;
                            this.dgVar.Rows[e.RowIndex].Cells[2].Style.BackColor = colorChoosed;
                            DisPlayObj(tmpvar);
                        }
                    }


                }


            }
        }

        void DisPlayObj(ClsVar vardata)
        {
            DrawMode_Save();
            HOperatorSet.SetDraw(hSmartWindowControl1.HalconWindow, vardata.RegionDrawMode);
            HOperatorSet.SetColor(hSmartWindowControl1.HalconWindow, vardata.Color);
            if (vardata.VarType == enumVarType.TUPLE3D)
            {

                //HOperatorSet.DispObjectModel3d(hSmartWindowControl1.HalconWindow, vardata.DataHTupe, new HTuple(), new HTuple(), new HTuple(), new HTuple());
            }
            else
            {
                HOperatorSet.DispObj(vardata.Data, hSmartWindowControl1.HalconWindow);

            }
            DrawMode_ReStore();

        }
        private void dgVar_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {

                string vname = this.dgVar.Rows[e.RowIndex].Cells[0].Value.ToString();
                if (this.clsVarsDict.ContainsKey(vname))
                {
                    var tmpvar = this.clsVarsDict[vname];

                    if (tmpvar.VarType != enumVarType.IMG)
                    {
                        if (tmpvar.VarType == enumVarType.REGION || tmpvar.VarType == enumVarType.XLD)
                        {
                            DisPlayObj(tmpvar);
                        }
                        else if (tmpvar.VarType == enumVarType.TUPLE)
                        {
                            HtupleForm htupleForm = new HtupleForm(tmpvar.DataHTupe);
                            htupleForm.Text = tmpvar.Name;
                            htupleForm.Show();
                        }
                        else if (tmpvar.VarType == enumVarType.TUPLE3D)
                        {
                            View_3D view_3D = new View_3D(tmpvar.DataHTupe, tmpvar.Name, tmpvar.Color);
                            view_3D.Text = tmpvar.Name;
                            view_3D.Show();
                        }
                        else if (tmpvar.VarType == enumVarType.EMPTY)
                        {
                            MessageBox.Show("空对象");
                        }

                    }
                    else
                    {
                        backImage = tmpvar.Data;
                        HOperatorSet.DispObj(backImage, hSmartWindowControl1.HalconWindow);
                    }
                }


            }
        }

        void ClearAllVar(Dictionary<string, ClsVar> clsVars)
        {
            foreach (var item in clsVars)
            {
                item.Value.Data?.Dispose();
            }
            clsVars.Clear();
            this.dgVar.Rows.Clear();
        }

        private void 合适图片ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            if (IsEmptyObject(backImage))
            {
                MessageBox.Show("没有图像");
                return;
            }
            this.hSmartWindowControl1.SetFullImagePart();



        }

        private void toolStripComboBox1_Click(object sender, EventArgs e)
        {

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                this.TopMost = true;
            }
            else
            {
                this.TopMost = false;
            }
        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.dgVar.SelectedCells.Count > 0)
            {
                FolderBrowserDialog dialog = new FolderBrowserDialog();
                dialog.Description = "请选择文件路径";
                DialogResult result = dialog.ShowDialog();
                if (result == System.Windows.Forms.DialogResult.Cancel)
                {
                    return;
                }
                string folderPath = dialog.SelectedPath.Trim();
                DirectoryInfo theFolder = new DirectoryInfo(folderPath);
                if (!theFolder.Exists)
                {
                    //操作
                    return;
                }
                foreach (DataGridViewCell item in this.dgVar.SelectedCells)
                {
                    string vname = this.dgVar.Rows[item.RowIndex].Cells[0].Value.ToString();
                    if (this.clsVarsDict.ContainsKey(vname))
                    {
                        var tmpvar = this.clsVarsDict[vname];
                        SaveVarData(tmpvar, folderPath);

                    }
                }


            }
            else
            {
                MessageBox.Show("请先选择要保存的变量");
            }
        }

        private void 用Halcon打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 重命名ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.dgVar.SelectedCells.Count > 0)
            {

                foreach (DataGridViewCell item in this.dgVar.SelectedCells)
                {
                    string vname = this.dgVar.Rows[item.RowIndex].Cells[0].Value.ToString();
                    string tmpname = vname;
                    DialogResult dialogResult = InputBox.ShowInputBox("重命名", "请输入新名字", ref tmpname);
                    if (vname.Equals(tmpname))
                    {
                        MessageBox.Show("输入的名称和以前的一致,修改失败");
                    }

                    if (dialogResult == DialogResult.OK)
                    {
                        if (!string.IsNullOrEmpty(tmpname))
                        {
                            this.dgVar.Rows[item.RowIndex].Cells[0].Value = tmpname;
                            if (clsVarsDict.ContainsKey(vname))
                            {
                                ClsVar Rename = new ClsVar(clsVarsDict[vname]);
                                Rename.Name = tmpname;
                                clsVarsDict.Remove(vname);//辞旧，迎新
                                clsVarsDict.Add(tmpname, Rename);
                            }

                        }

                    }

                }


            }
            else
            {
                MessageBox.Show("请先选择变量");
            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.dgVar.SelectedCells.Count > 0)
            {

                foreach (DataGridViewCell item in this.dgVar.SelectedCells)
                {
                    string vname = this.dgVar.Rows[item.RowIndex].Cells[0].Value.ToString();
                    this.dgVar.Rows.RemoveAt(item.RowIndex);
                    if (clsVarsDict.ContainsKey(vname))
                    {
                        clsVarsDict.Remove(vname);
                    }

                }


            }
            else
            {
                MessageBox.Show("请先选择变量");
            }
        }

        public bool IsEmptyObject(HObject hObject)
        {
            HTuple isNull = 0; HObject nullobject = new HObject();
            bool value = false;
            HOperatorSet.GenEmptyObj(out nullobject);
            try
            {
                HOperatorSet.TestEqualObj(nullobject, hObject, out isNull);
            }
            catch (Exception)
            {
                value = true;
            }

            if (isNull == 1)
            {
                value = true;
            }
            isNull.Dispose(); nullobject.Dispose();
            return value;
        }
    }
}
